package com.weiqiao.evaluate.rest;


import com.google.gson.Gson;
import com.weiqiao.aop.log.Log;
import com.weiqiao.evaluate.domain.Evluate4Entity;
import com.weiqiao.evaluate.service.Evluate4Service;
import com.weiqiao.utils.PageUtil;
import com.weiqiao.utils.SecurityUtils;
import com.weiqiao.utils.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.regex.Pattern;

@RestController
@RequestMapping("api")
public class Evluate4Controller {

    /**
    * 业务层
    */
    @Autowired
    private Evluate4Service evluate4Service;


    /**
    * 获取列表
    * @param request
    * @param pageable
    * @return
    */
    @Log("查询Evluate4")
    @GetMapping(value = "/evluate4")
    public ResponseEntity getList(HttpServletRequest request, Pageable pageable) {
        int company_id = SecurityUtils.getCompanyId();
        //组装查询条件
        Bson filter = this.getEvluate4ParameterDocument(request);
        int count =  evluate4Service.countNum(company_id, filter);
        List<Evluate4Entity> list = evluate4Service.findList(company_id, filter, pageable);
        return new ResponseEntity(PageUtil.toPage(list, count), HttpStatus.OK);
    }

    /**
    * 封装列表查询条件
    * @param request
    * @return
    */
    private Bson getEvluate4ParameterDocument(HttpServletRequest request) {

        Document doc = new Document("delete_flag", 0);
        String evaluateId = request.getParameter("evaluateId");
        if (!StringUtils.isBlank(evaluateId)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + evaluateId + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("evaluateId", pattern);
                }
        String nick = request.getParameter("nick");
        if (!StringUtils.isBlank(nick)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + nick + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("nick", pattern);
                }
        String iconUrl = request.getParameter("iconUrl");
        if (!StringUtils.isBlank(iconUrl)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + iconUrl + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("iconUrl", pattern);
                }
        String level = request.getParameter("level");
        if (!StringUtils.isBlank(level)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + level + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("level", pattern);
                }
        String createDate = request.getParameter("createDate");
        if (!StringUtils.isBlank(createDate)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + createDate + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("createDate", pattern);
                }
        String state = request.getParameter("state");
        if (!StringUtils.isBlank(state)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + state + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("state", pattern);
                }
        String proTitle = request.getParameter("proTitle");
        if (!StringUtils.isBlank(proTitle)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + proTitle + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("proTitle", pattern);
                }
        String deleteFlag = request.getParameter("deleteFlag");
        if (!StringUtils.isBlank(deleteFlag)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + deleteFlag + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("deleteFlag", pattern);
                }
        String activityDetails = request.getParameter("activityDetails");
        if (!StringUtils.isBlank(activityDetails)) {
        // 模糊
        Pattern pattern = Pattern.compile("^.*" + activityDetails + ".*$", Pattern.CASE_INSENSITIVE);
        doc.append("activityDetails", pattern);
                }
        return doc;
    }

    @Log("获取详情Evluate4")
    @PostMapping(value = "/evluate4/{id}")
    public ResponseEntity getOne(@PathVariable String id) {
        int company_id = SecurityUtils.getCompanyId();
        Evluate4Entity entity = evluate4Service.getEntity(company_id, new Document("create_date", id));
        return new ResponseEntity(entity, HttpStatus.OK);
    }

    /**
    * 更新、添加
    *
    */
    @Log("编辑Evluate4")
    @PutMapping(value = "/evluate4")
    public ResponseEntity edit(@RequestBody Evluate4Entity entity) {
        int company_id = SecurityUtils.getCompanyId();
        if (!StringUtils.isBlank(entity.getCreate_date())) {
            Bson filter = new Document("create_date", entity.getCreate_date());
            //修改
            evluate4Service.updateInfo(company_id, filter, Document.parse(new Gson().toJson(entity)));
        } else {
            //添加
            entity.setCreate_date(String.valueOf(System.currentTimeMillis()));
            // 未删除
            entity.setDelete_flag(0);
            evluate4Service.addInfo(company_id, new Gson().toJson(entity));
        }
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除Evluate4")
    @DeleteMapping(value = "/evluate4/{id}")
    public ResponseEntity delete(@PathVariable String id){
        int company_id = SecurityUtils.getCompanyId();
        Bson filter = new Document("evaluate_id", id);
        //删除 假删除
        Document doc = new Document("delete_flag", 1);
        evluate4Service.updateInfo(company_id, filter, doc);
        return new ResponseEntity(HttpStatus.OK);
    }
}
